package com.itaem.community.modules.repair.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.modules.repair.entity.PicturesEntity;
import com.itaem.community.modules.repair.service.impl.PicturesServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


/**
 * <p>
 * 图片 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
  
@Api(tags = "图片模块")
@ResponseResult
@RestController
@RequestMapping("/pictures")
public class PicturesController {

    @Autowired
    private PicturesServiceImpl picturesService;

    /**
     *
     * @param rid 获取RepairId 为 rid 的所有图片
     * @return 图片的List集合
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"},logical = Logical.OR)
    @ApiOperation("获取报修单所有图片")
    @GetMapping("/{rid}")
    public List<PicturesEntity> info(@PathVariable Long rid){
        return picturesService.list(new QueryWrapper<PicturesEntity>()
                .lambda()
                .eq(PicturesEntity::getRepairId,rid));
    }

    /**
     *
     * @param image 前端传入的图片对象
     * @param rid 图片对应的RepairId
     * @param pid 此图片是第pid张图片 (上限为9)
     * @return
     * @throws IOException
     */
//    @RequiresRoles(value = {"超级管理员","业主"},logical = Logical.OR)
    @ApiOperation("新增")
    @PostMapping("/{rid}/{pid}")
    public Boolean save(@RequestBody MultipartFile image,@PathVariable Long rid,@PathVariable Long pid) throws IOException {
        if(image.isEmpty()) {return false;}
        String originalFilename = image.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String prefix = "C:\\Users\\yangyang\\Desktop\\pictures\\";
        String picUrl =  pid +"\\" + UUID.randomUUID().toString().replace("-","") + suffix;
        System.out.println(picUrl);
        PicturesEntity picturesEntity = new PicturesEntity();
        picturesEntity.setPicUrl(prefix+picUrl);
        picturesEntity.setDeleted(false);
        picturesEntity.setRepairId(rid);
        picturesService.save(picturesEntity);
        image.transferTo(new File(prefix+picUrl));
        return true;
    }

    /**
     *
     * @param rid 图片对应的RepairId
     * @param pid 此图片是第pid张图片 (上限为9)
     * @return
     */
//    @RequiresRoles(value = {"超级管理员","业主"},logical = Logical.OR)
    @ApiOperation("删除")
    @DeleteMapping("/{rid}/{pid}")
    public Boolean remove(@PathVariable Long rid,@PathVariable Long pid) {
        picturesService.update(new UpdateWrapper<PicturesEntity>()
                .lambda()
                .eq(PicturesEntity::getRepairId,rid)
                .like(PicturesEntity::getPicUrl,pid+"%")
                .set(PicturesEntity::getDeleted,true));
        return true;
    }
}

